﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class RolePurviewService
    {
        /// <summary>
        /// 查询角色权限资源信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RolePurview GetRolePurview(long id)
        {
            RolePurview rolePurview = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    rolePurview = new RolePurview();
                    rolePurview.Id = id;
                    rolePurview = new RolePurviewDAO().GetRolePurview(conn, rolePurview);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rolePurview;
        }

        /// <summary>
        /// 查询角色权限资源列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public DataTable GetRolePurviewTable(int pageindex, int pagesize, long roleid)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    RolePurview rolePurview = new RolePurview();
                    rolePurview.Pageindex = pageindex;
                    rolePurview.Pagesize = pagesize;
                    rolePurview.Roleid = roleid;
                    dt = new RolePurviewDAO().GetRolePurviewTable(conn, rolePurview);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询角色权限资源记录数
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public long GetRolePurviewRecord(long roleid)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    RolePurview rolePurview = new RolePurview();
                    rolePurview.Roleid = roleid;
                    record = new RolePurviewDAO().GetRolePurviewRecord(conn, rolePurview);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入角色权限资源信息
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="purviewid"></param>
        /// <returns></returns>
        public long InsertRolePurview(long roleid, long purviewid)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    RolePurview rolePurview = new RolePurview();
                    rolePurview.Roleid = roleid;
                    rolePurview.Purviewid = purviewid;
                    id = new RolePurviewDAO().InserRolePurview(conn, rolePurview);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 删除角色权限信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteRolePurview(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        RolePurviewDAO rolePurviewDAO = new RolePurviewDAO();
                        RolePurview rolePurview = new RolePurview();
                        foreach (String lopid in arr)
                        {
                            rolePurview.Id = Int64.Parse(lopid);
                            rolePurviewDAO.DeleteRolePurview(conn, rolePurview);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 删除角色权限
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteRolePurview(long roleid)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    RolePurview rolePurview = new RolePurview();
                    rolePurview.Roleid = roleid;
                    count = new RolePurviewDAO().DeleteRolePurview(conn, rolePurview);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 删除角色权限
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteRolePurview(DbConnection conn, long roleid)
        {
            int count = 0;
            try
            {
                RolePurview rolePurview = new RolePurview();
                rolePurview.Roleid = roleid;
                count += new RolePurviewDAO().DeleteRolePurview(conn, rolePurview);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
